package com.kmair.caiwu.attendance.services;


import com.kmair.caiwu.attendance.dao.JiabanMapper;
import com.kmair.caiwu.attendance.pojo.Jiaban;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class JiabanService {
    private final JiabanMapper dao;

    @Autowired
    public JiabanService(JiabanMapper dao) {
        this.dao = dao;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void saveTotaljiabanDyas() {
        dao.saveTolalJiabanDays();
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public int insert(Jiaban recourd) {
        recourd.setJiabanDays(differentDaysByMillisecond(recourd.getBeginDate(), recourd.getEndDate()) + 1);
        return dao.insert(recourd);
    }

    public List<Jiaban> selectSelective(Jiaban recourd) {
        return dao.selectSelective(recourd);
    }

    /**
     * 通过时间秒毫秒数判断两个时间的间隔
     */
    private int differentDaysByMillisecond(Date date1, Date date2) {
        return (int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24));
    }
}
